package cn.stylefeng.guns.modular.gsb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.gsb.entity.Gsb;
import cn.stylefeng.guns.modular.gsb.enums.GsbExceptionEnum;
import cn.stylefeng.guns.modular.gsb.mapper.GsbMapper;
import cn.stylefeng.guns.modular.gsb.pojo.request.GsbRequest;
import cn.stylefeng.guns.modular.gsb.service.GsbService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公司表业务实现层
 *
 * @author zhu
 * @date 2021/10/06 11:04
 */
@Service
public class GsbServiceImpl extends ServiceImpl<GsbMapper, Gsb> implements GsbService {

	@Override
    public void add(GsbRequest gsbRequest) {
        Gsb gsb = new Gsb();
        BeanUtil.copyProperties(gsbRequest, gsb);
        this.save(gsb);
    }

    @Override
    public void del(GsbRequest gsbRequest) {
        Gsb gsb = this.queryGsb(gsbRequest);
        this.removeById(gsb.getId());
    }

    @Override
    public void edit(GsbRequest gsbRequest) {
        Gsb gsb = this.queryGsb(gsbRequest);
        BeanUtil.copyProperties(gsbRequest, gsb);
        this.updateById(gsb);
    }

    @Override
    public Gsb detail(GsbRequest gsbRequest) {
        return this.queryGsb(gsbRequest);
    }

    @Override
    public PageResult<Gsb> findPage(GsbRequest gsbRequest) {
        LambdaQueryWrapper<Gsb> wrapper = createWrapper(gsbRequest);
        Page<Gsb> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Gsb> findList(GsbRequest gsbRequest) {
        LambdaQueryWrapper<Gsb> wrapper = this.createWrapper(gsbRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhu
     * @date 2021/10/06 11:04
     */
    private Gsb queryGsb(GsbRequest gsbRequest) {
        Gsb gsb = this.getById(gsbRequest.getId());
        if (ObjectUtil.isEmpty(gsb)) {
            throw new BusinessException(GsbExceptionEnum.GSB_NOT_EXISTED);
        }
        return gsb;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhu
     * @date 2021/10/06 11:04
     */
    private LambdaQueryWrapper<Gsb> createWrapper(GsbRequest gsbRequest) {
        LambdaQueryWrapper<Gsb> queryWrapper = new LambdaQueryWrapper<>();

        Long id = gsbRequest.getId();
        String gsbh = gsbRequest.getGsbh();
        String gsmc = gsbRequest.getGsmc();
        String gsbz = gsbRequest.getGsbz();
        String gsacount = gsbRequest.getGsacount();
        String createTime = gsbRequest.getCreateTime();
        Long createUser = gsbRequest.getCreateUser();
        String updateTime = gsbRequest.getUpdateTime();
        Long updateUser = gsbRequest.getUpdateUser();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Gsb::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(gsbh), Gsb::getGsbh, gsbh);
        queryWrapper.like(ObjectUtil.isNotEmpty(gsmc), Gsb::getGsmc, gsmc);
        queryWrapper.like(ObjectUtil.isNotEmpty(gsbz), Gsb::getGsbz, gsbz);
        queryWrapper.like(ObjectUtil.isNotEmpty(gsacount), Gsb::getGsacount, gsacount);
        queryWrapper.eq(ObjectUtil.isNotNull(createTime), Gsb::getCreateTime, createTime);
        queryWrapper.eq(ObjectUtil.isNotNull(createUser), Gsb::getCreateUser, createUser);
        queryWrapper.eq(ObjectUtil.isNotNull(updateTime), Gsb::getUpdateTime, updateTime);
        queryWrapper.eq(ObjectUtil.isNotNull(updateUser), Gsb::getUpdateUser, updateUser);

        return queryWrapper;
    }

}